import datetime
import sys
import traceback

from bs4 import BeautifulSoup
from xjlibrary.database_moudel.simple.mysqlclient import MySqlDbConnect, SelctSqlFromDB, ExeSqlToDB
from xjlibrary.mdatetime.mtime import StringToDate2, getnextmonth, dateToString
from xjlibrary.mrequest import baserequest
from xjlibrary.myredis.myredisclient import getDataFromRedis
from xjlibrary.our_file_dir.base_dir import BaseDir

HEARDER = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
    "Cache-Control": "no-cache",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "epub.sipo.gov.cn",
    "Origin": "http://epub.sipo.gov.cn",
    "Pragma": "no-cache",
    "Proxy-Connection": "keep-alive",
    "Referer": "http://epub.sipo.gov.cn/gjcx.jsp",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"
}

url = "http://epub.sipo.gov.cn/patentoutline.action"

postdata = {
    "showType": "1",
    "strSources": "",
    "strWhere": "OPD=BETWEEN['1985.09.01','1985.10.01']",  # 日期
    "numSortMethod": "",
    "strLicenseCode": "",
    "numIp": "0",
    "numIpc": "",
    "numIg": "0",
    "numIgc": "",
    "numIgd": "",
    "numUg": "0",
    "numUgc": "",
    "numUgd": "",
    "numDg": "0",
    "numDgc": "",
    "pageSize": "10",  # 每页显示量 3或者10
    "pageNow": "1"  # 此处为page
}

listproxy = []
curPath = BaseDir.get_file_dir_absolute(__file__)


# 数据库链接
def MajorDbConnect():
    return MySqlDbConnect(curPath, 'db.ini')


def selectFromDB():
    sql = "select endtime from timeeapo order by endtime DESC limit 1"
    rows = SelctSqlFromDB(sql, MajorDbConnect())
    return rows


def SelectProxy():
    sSql = "SELECT `proxy` FROM `proxy_pool`"
    # 数据库连接
    conn = MajorDbConnect()
    rows = SelctSqlFromDB(sSql, conn)
    return rows


def readDBProxy():
    return getDataFromRedis(curPath, 'db.ini')


def downpage(postdata):
    global listproxy
    while True:
        if listproxy == []:
            listproxy = list(readDBProxy())
            if listproxy:
                proxy = listproxy.pop()
            else:
                continue
        else:
            proxy = listproxy.pop()
        proxys = {
            "http": proxy,
            "https": proxy
        }
        print(proxys)
        BoolResult, errString, r = baserequest.BaseRequestPost(url, data=postdata, headers=HEARDER, mark="w790 right",
                                                               proxies=proxys,
                                                               timeout=(30, 60))
        if BoolResult:
            break
    return r


def parahtml(r):
    soup = BeautifulSoup(r.text, 'lxml')
    div_tag = soup.find("div", class_="next")
    all_a_tag = div_tag.find_all("a")
    a_tag = all_a_tag[-2]
    allpagenum = "".join(a_tag.stripped_strings)
    print(allpagenum)
    try:
        int(allpagenum)
    except:
        print("解析错误 请检查后继续")
        sys.exit(-1)
    return allpagenum


def main():
    rows = selectFromDB()
    if rows:
        starttime = rows[0][0].split(".")[:-1]
        starttime.append('01')
        starttime = ".".join(starttime)
        endtime = dateToString(getnextmonth(StringToDate2(starttime), 2))
        if getnextmonth(StringToDate2(starttime)) + datetime.timedelta(10) > datetime.datetime.combine(
                datetime.date.today(),
                datetime.datetime.min.time()):
            return "over"
        else:
            postdata["strWhere"] = "OPD=BETWEEN['{}','{}']".format(starttime, endtime)
            r = downpage(postdata)
            pagenum = parahtml(r)
            sql = "insert into timeeapo(`starttime`,`endtime`,`allpage`) values('{}','{}','{}')".format(starttime,
                                                                                                        endtime,
                                                                                                        pagenum)
            ExeSqlToDB(sql, MajorDbConnect())
    else:
        postdata["strWhere"] = "OPD=BETWEEN['1985.09.01','1985.10.01']"
        starttime = "1985.09.01"
        endtime = "1985.10.02"
        r = downpage(postdata)
        pagenum = parahtml(r)
        sql = "insert into timeeapo(`starttime`,`endtime`,`allpage`) values('{}','{}','{}')".format(starttime, endtime,
                                                                                                    pagenum)
        ExeSqlToDB(sql, MajorDbConnect())


if __name__ == "__main__":
    while True:
        try:
            result = main()
            if result == "over":
                print("over")
                break
        except:
            traceback.format_exc()
